define(["jquery", "jquery-cookie"], function ($) {
    // 勾选协议
    function tickAgreement() {
        $('#tick').add('#tickLabel').click(function () {
            $('#tick').find('i').toggleClass('tick-icon');
        });
    }

    // 取到账号和密码发送给后端
    function sendId() {
        // 如果tip出现, 按关闭可以关掉
        $('#closeIcon').on('click', function () {
            $('#tipBox').addClass('visibility-hidden');
        });

        // 如果手机号为空, 则不允许提交
        if ($('#accountInput').val() === '') {
            $('#btn').attr({
                disabled: 'disabled'
            });
        }

        $('#accountInput').on('keyup', function () {
            if (!(/^1[3456789]\d{9}$/.test($('#accountInput').val())) || $('#accountInput').val() === '') {
                $('#tipBox').removeClass('visibility-hidden');
                $('#btn').attr({
                    disabled: 'disabled'
                });
            } else {
                $('#tipBox').addClass('visibility-hidden');
                $('#btn').removeAttr('disabled');
                $('#pwdInput').on('blur', function () {
                    pwdNull();
                });
                send();
            }
        });

        // 点击注册发送请求
        function send() {
            $('#btn').on('click', function (e) {
                e = e || window.event;

                var account = $('#accountInput').val();
                var pwd = $('#pwdInput').val();

                // 向后端发送ajax请求
                $.ajax({
                    type: 'post',
                    url: '../api/user/register.php',
                    data: {
                        account: account,
                        pwd: pwd
                    },
                    success: function (body, statusText, xhr) {
                        // 后台返回的数据
                        console.log(body);
                        // console.log(statusText); // success, 成功
                        // console.log(xhr);   // xhr.readyState === 4 和 xhr.status === 200
                        // 把字符串转成数组
                        var resp = JSON.parse(body);
                        if (resp.code == 1) {
                            alert(resp.msg);
                            location.replace('./login.html');
                        } else if (resp.code == 2) {
                            console.log(resp.msg);
                            $('#tipBox').removeClass('visibility-hidden');
                            $('#tipTxt').html(resp.msg);
                        } else if (resp.code == 0) {
                            console.log(resp.msg);
                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                });

                // 阻止默认事件
                e.preventDefault();
            });
        }

        // 如果密码为空, 提示请输入密码
        function pwdNull() {
            if ($('#pwdInput').val() === '') {
                $('#tipBox').removeClass('visibility-hidden');
                $('#tipTxt').html('请输入密码');
                $('#btn').attr({
                    disabled: 'disabled'
                });
            } else {
                $('#tipBox').addClass('visibility-hidden');
                $('#btn').removeAttr('disabled');
            }
        }

    }

    return {
        tickAgreement: tickAgreement,
        sendId: sendId
    }
})